package com.project;

import java.io.File;
import java.io.IOException;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;

import android.app.Activity;
//import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
//import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
//import android.os.Environment;
import android.provider.MediaStore;
//import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
//import android.widget.Toast;
import android.widget.Toast;

// TODO: Auto-generated Javadoc
/**
 * The Class CaptureActivity.
 */
public class CaptureActivity extends Activity {
	
	/** The btn capture. */
	private Button btnCapture;
	
	/** The choose gallery. */
	private Button chooseGallery;
	
	/** The pin button. */
	private Button pinButton;
	
	/** The edit name. */
	private EditText editName;
	
	/** The edit des. */
	private EditText editDes;
	
	/** The imageview. */
	private ImageView imageview;
	
	/** The Constant SELECT_PICTURE. */
	private static final int SELECT_PICTURE = 1;
	
	/** The Constant CAPTURE_PICTURE. */
	private static final int CAPTURE_PICTURE = 2;
	
	/** The file path. */
	private String filePath = ""; 
	
	/** The image uri. */
	private Uri imageUri;

	/** The geo lat. */
	Double geoLat;
	
	/** The geo lng. */
	Double geoLng;
	
	/** The db helper. */
	DBHelper dbHelper;
	
	/** The location manager. */
	LocationManager locationManager;
	
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.capture);
		
		imageview = (ImageView)findViewById(R.id.showImage);
		
		//Toast.makeText(this, "start", Toast.LENGTH_SHORT).show();
		dbHelper = new DBHelper(this);
		
		String context = Context.LOCATION_SERVICE;
		locationManager = (LocationManager) getSystemService(context);
		
		Criteria criteria = new Criteria();
		criteria.setAccuracy(Criteria.ACCURACY_FINE);
		criteria.setAltitudeRequired(false);
		criteria.setBearingRequired(false);
		criteria.setCostAllowed(true);
		criteria.setPowerRequirement(Criteria.POWER_LOW);
		
		String provider = locationManager.getBestProvider(criteria, true);
		Location location = locationManager.getLastKnownLocation(provider);
		
		try{
			geoLat = location.getLatitude() * 1E6;
			geoLng = location.getLongitude() * 1E6;
			//Toast.makeText(this, ""+geoLat+" , "+geoLng, Toast.LENGTH_LONG).show();
		}catch(NullPointerException e){
			//Toast.makeText(this, "Null", Toast.LENGTH_LONG).show();
		}
		
		// Camara Button
		chooseGallery = (Button)findViewById(R.id.chooseGallery);
		btnCapture = (Button)findViewById(R.id.btnCapture);
		editName = (EditText)findViewById(R.id.editName);
		editDes = (EditText)findViewById(R.id.editDescription);
		imageview =(ImageView)findViewById(R.id.showImage);
		
		btnCapture.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				String fileName = "temp.jpg";  
		       ContentValues values = new ContentValues();  
		       values.put(MediaStore.Images.Media.TITLE, fileName);  
		       imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
		 
		       Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
		       intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
		       startActivityForResult(intent, CAPTURE_PICTURE);
			}
		});

// Gallery Button		
		chooseGallery.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {
				Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(Intent.createChooser(intent,
                        "Select Picture"), SELECT_PICTURE);
			}
		});
		
// Pin Button
		pinButton = (Button)findViewById(R.id.pin);
		pinButton.setOnClickListener(new OnClickListener(){
			public void onClick(View v) {				
				
				if(filePath == "" || editName.getText().toString().trim() == "" || editDes.getText().toString().trim() == ""){
					//Toast.makeText(v.getContext(), "Data not found !!! ", Toast.LENGTH_LONG).show();
					return;
				}
				Intent intent = new Intent(CaptureActivity.this, Preview.class);
				intent.putExtra(Preview.IMAGE_URI, filePath);
				intent.putExtra("name", editName.getText().toString());
				intent.putExtra("description", editDes.getText().toString());
				startActivity(intent);
			}
		});
	}



	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		File file;
		if (resultCode == RESULT_OK) {
		   	switch (requestCode) {
		case SELECT_PICTURE:
		   imageUri = data.getData();
//Image Select.
		   filePath = getPath(imageUri);

		   Toast.makeText(this, imageUri+"", Toast.LENGTH_LONG).show();
//Set Image
		   imageview.setImageURI(imageUri);
		   //Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();
		   break;
		case CAPTURE_PICTURE:
		String[] projection = { MediaStore.Images.Media.DATA}; 
		           Cursor cursor = managedQuery(imageUri, projection, null, null, null); 
		           int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
		           cursor.moveToFirst(); 
		           
		 

//Get Path
		           filePath = cursor.getString(column_index_data);
		 
//Set Image
		           imageview.setImageURI(imageUri);
		           //Toast.makeText(this, filePath, Toast.LENGTH_LONG).show();
		           break;
		default:
		}
		}
		 
	}
		
	/**
	 * Gets the path.
	 *
	 * @param uri the uri
	 * @return the path
	 */
	public String getPath(Uri uri) {
			   String[] projection = { MediaStore.Images.Media.DATA };
			   Cursor cursor = managedQuery(uri, projection, null, null, null);
			   int column_index = cursor
			           .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			   cursor.moveToFirst();
			   return cursor.getString(column_index);
			}

}
